#include <isa.h>
#include <cpu/cpu.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "sdb.h"
#include "memory/paddr.h"

static int is_batch_mode = false;
word_t expr(char *e, bool *success);
void init_regex();
void init_wp_pool();
void print_wp();
void free_wp(int NO);
void new_wp(char *expr,word_t value);

/* We use the `readline' library to provide more flexibility to read from stdin. */
static char* rl_gets() {
  static char *line_read = NULL;

  if (line_read) {
    free(line_read);
    line_read = NULL;
  }

  line_read = readline("(nemu) ");

  if (line_read && *line_read) {
    add_history(line_read);
  }

  return line_read;
}

static int cmd_c(char *args) {
  cpu_exec(-1);
  return 0;
}


static int cmd_q(char *args) {
  return -1;
}

static int cmd_help(char *args);

static int cmd_si(char *args);


static int cmd_info(char *args)
 {
  /* extract the first argument */
  char *arg = strtok(NULL, " ");

  if (strcmp(arg,"r")==0) {
	isa_reg_display();
  }
  else if (strcmp(arg,"w")==0){
  	print_wp();
  }
  else{
  	printf("please enter r or w\n");
  }

	 
return 0;
}
static int cmd_x(char *args)
 {
	int i;  
	int len;
paddr_t address;
 /* extract the first argument */
  char *arg = strtok(NULL, " ");
  char *EXPR =strtok(NULL," ");
  sscanf(arg,"%d",&len);
  sscanf(EXPR,"%x",&address);
 
for(i=0;i<len;i++){
printf("0x%x\t:%08lx\n",address,paddr_read(address,1));
address+=8;
}


  return 0;
}

static int cmd_p (char *args);
static int cmd_d (char *args);
static int cmd_w (char *args);
static struct {
  const char *name;
  const char *description;
  int (*handler) (char *);
} cmd_table [] = {
  { "help", "Display informations about all supported commands", cmd_help },
  { "c", "Continue the execution of the program", cmd_c },
  { "q", "Exit NEMU", cmd_q },
  { "si", "exec one time",cmd_si },
  { "info", "show exe statu", cmd_info},
  { "x", "scan memory", cmd_x},
  { "p", "calculate expr", cmd_p},
  {"d", "deleta a watch point",cmd_d},
  {"w", "creat a new watch point",cmd_w}
  /* TODO: Add more commands */

};

#define NR_CMD ARRLEN(cmd_table)


static int cmd_help(char *args) {
  /* extract the first argument */
  char *arg = strtok(NULL, " ");
  int i;

  if (arg == NULL) {
    /* no argument given */
    for (i = 0; i < NR_CMD; i ++) {
      printf("%s - %s\n", cmd_table[i].name, cmd_table[i].description);
    }
  }
  else {
    for (i = 0; i < NR_CMD; i ++) {
      if (strcmp(arg, cmd_table[i].name) == 0) {
        printf("%s - %s\n", cmd_table[i].name, cmd_table[i].description);
        return 0;
      }
    }
    printf("Unknown command '%s'\n", arg);
  }
  return 0;
}

void sdb_set_batch_mode() {
  is_batch_mode = true;
}

void sdb_mainloop() {
  if (is_batch_mode) {
    cmd_c(NULL);
    return;
  }

  for (char *str; (str = rl_gets()) != NULL; ) {
    char *str_end = str + strlen(str);

    /* extract the first token as the command */
    char *cmd = strtok(str, " ");
    if (cmd == NULL) { continue; }

    /* treat the remaining string as the arguments,
     * which may need further parsing
     */
    char *args = cmd + strlen(cmd) + 1;//ZHENG:get next value
    if (args >= str_end) {
      args = NULL;
    }

#ifdef CONFIG_DEVICE
    extern void sdl_clear_event_queue();
    sdl_clear_event_queue();
#endif

    int i;
    for (i = 0; i < NR_CMD; i ++) {
      if (strcmp(cmd, cmd_table[i].name) == 0) {
        if (cmd_table[i].handler(args) < 0) { nemu_state.state = NEMU_QUIT;return; } //here return if cmd_q
        break;
      }
    }

    if (i == NR_CMD) { printf("Unknown command '%s'\n", cmd); }
  }
}

void init_sdb() {
  /* Compile the regular expressions. */
  init_regex();

  /* Initialize the watchpoint pool. */
  init_wp_pool();
}

static int cmd_si(char *args){	
  /* extract the first argument */
  char *arg = strtok(NULL, " ");
  int i;

  if (arg == NULL) {
    /* no argument given */
	cpu_exec(1);
  }
  else{
  sscanf(arg,"%d",&i);
  cpu_exec(i);
  }

  return 0;
}


static int cmd_p (char *args){
bool success = true;
char *arg = strtok(NULL,"");
printf("%s\n",arg);
expr(arg,&success);
return 0;
}

static int cmd_d(char *args){
  char *arg = strtok(NULL, " ");
  int i;

  if (arg == NULL) {
    printf("Please enter a number after d");
  }
  else{
  sscanf(arg,"%d",&i);
  free_wp(i);
  }

  return 0;

}

static int cmd_w(char *args){
	bool success = true;
	uint64_t value = 0;
	char *arg = strtok(NULL,"");
	value =	expr(arg,&success);
//	printf("%ld\n",value);
	new_wp(arg,value);	
	return 0;
}

